package cn.community.care.module.system.service.purchaseinfo;

import cn.community.care.module.system.dal.dataobject.user.AdminUserDO;
import cn.community.care.module.system.dal.mysql.user.AdminUserMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;
import cn.community.care.module.system.controller.admin.purchaseinfo.vo.*;
import cn.community.care.module.system.dal.dataobject.purchaseinfo.PurchaseInfoDO;
import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.common.pojo.PageParam;
import cn.community.care.framework.common.util.object.BeanUtils;

import cn.community.care.module.system.dal.mysql.purchaseinfo.PurchaseInfoMapper;

import static cn.community.care.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.community.care.module.system.enums.ErrorCodeConstants.*;

/**
 * 采购信息 Service 实现类
 *
 * @author 美妆管理员
 */
@Service
@Validated
public class PurchaseInfoServiceImpl implements PurchaseInfoService {

    @Resource
    private PurchaseInfoMapper purchaseInfoMapper;

    @Resource
    private AdminUserMapper userMapper;

    @Override
    public Long createPurchaseInfo(PurchaseInfoSaveReqVO createReqVO) {
        // 插入
        PurchaseInfoDO purchaseInfo = BeanUtils.toBean(createReqVO, PurchaseInfoDO.class);
        //生成日期加随机数十六位编号
        String s = generateUniqueIdentifier();
        purchaseInfo.setPurchaseOrderNum(s);
        AdminUserDO adminUserDO = userMapper.selectById(createReqVO.getUserId());
        purchaseInfo.setPurchaserName(adminUserDO.getNickname());
        purchaseInfoMapper.insert(purchaseInfo);
        // 返回
        return purchaseInfo.getId();
    }
    public  String generateUniqueIdentifier() {
        // 获取当前日期，格式为yyyyMMdd
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
        String datePart = dateFormat.format(new Date());

        // 生成一个8位的随机数
        Random random = new Random();
        int randomPart = 10000000 + random.nextInt(90000000);

        // 拼接日期和随机数
        return datePart + String.valueOf(randomPart);
    }


    @Override
    public void updatePurchaseInfo(PurchaseInfoSaveReqVO updateReqVO) {
        // 校验存在
        validatePurchaseInfoExists(updateReqVO.getId());
        // 更新
        PurchaseInfoDO updateObj = BeanUtils.toBean(updateReqVO, PurchaseInfoDO.class);
        purchaseInfoMapper.updateById(updateObj);
    }

    @Override
    public void deletePurchaseInfo(Long id) {
        // 校验存在
        validatePurchaseInfoExists(id);
        // 删除
        purchaseInfoMapper.deleteById(id);
    }

    private void validatePurchaseInfoExists(Long id) {
        if (purchaseInfoMapper.selectById(id) == null) {
//            throw exception(PURCHASE_INFO_NOT_EXISTS);
        }
    }

    @Override
    public PurchaseInfoDO getPurchaseInfo(Long id) {
        return purchaseInfoMapper.selectById(id);
    }

    @Override
    public PageResult<PurchaseInfoDO> getPurchaseInfoPage(PurchaseInfoPageReqVO pageReqVO) {
        return purchaseInfoMapper.selectPage(pageReqVO);
    }

}